﻿namespace Ocelot.Admin.Api.Application
{
    public class GlobalConfigurationService : IGlobalConfigurationService, ITransient
    {
        private readonly SqlSugarScopeProvider _sqlserverDB;
        public GlobalConfigurationService(ISqlSugarClient db)
        {
            _sqlserverDB = db.AsTenant().GetConnectionScope(DatabaseConfigIdConst.OcelotSettings);
        }

        #region 查询

        /// <summary>
        /// 查询全局配置信息
        /// </summary>
        /// <returns></returns>
        public async Task<GlobalConfiguration> GetGlobalConfigurationAsync()
        {
            return await _sqlserverDB.Queryable<GlobalConfiguration>().With(SqlWith.NoLock).FirstAsync();
        }

        #endregion

        #region 新增

        /// <summary>
        /// 新增全局配置信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<int> InsertGlobalConfigurationAsync(InsertGlobalConfigurationRequest request)
        {
            var GlobalConfiguration = request.Adapt<GlobalConfiguration>();
            var insertResult = await _sqlserverDB.Insertable(GlobalConfiguration).ExecuteReturnIdentityAsync();
            if (insertResult > 0)
            {
                return insertResult;
            }
            else
            {
                throw Oops.Oh(ErrorCodes.INSERT_GLOBAL_CONFIGURATION_FAIL).StatusCode((int)ErrorCodes.INSERT_GLOBAL_CONFIGURATION_FAIL);
            }
        }

        #endregion

        #region 编辑

        /// <summary>
        /// 更新全局配置信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<string> UpdateGlobalConfigurationAsync(UpdateGlobalConfigurationRequest request)
        {
            var GlobalConfiguration = request.Adapt<GlobalConfiguration>();
            var result = await _sqlserverDB.Updateable(GlobalConfiguration)
                   .ExecuteCommandHasChangeAsync();
            if (result)
            {
                return "编辑全局配置成功";
            }
            else
            {
                throw Oops.Oh(ErrorCodes.UPDATE_GLOBAL_CONFIGURATION_FAIL).StatusCode((int)ErrorCodes.UPDATE_GLOBAL_CONFIGURATION_FAIL);
            }
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除全局配置信息
        /// </summary>
        /// <param name="globalConfigurationId">全局配置Id</param>
        /// <returns></returns>
        public async Task<string> DeletGlobalConfigurationAsync(int globalConfigurationId)
        {
            var result = await _sqlserverDB.Deleteable<GlobalConfiguration>()
                .Where(it => it.Id == globalConfigurationId)
                .ExecuteCommandHasChangeAsync();
            if (result)
            {
                return "删除全局配置成功";
            }
            else
            {
                throw Oops.Oh(ErrorCodes.DELETE_GLOBAL_CONFIGURATION_FAIL).StatusCode((int)ErrorCodes.DELETE_GLOBAL_CONFIGURATION_FAIL);
            }
        }

        #endregion
    }
}